package com.test.common;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import msg.chestionar.ejb.bi.ChestionarBI;
import msg.chestionar.ejb.bi.RaspunsBI;

import com.msg.chestionar.to.ChestionarTO;
import com.msg.chestionar.to.IntrebareTO;
import com.msg.chestionar.to.RaspunsuriTO;

@ManagedBean(name = "rezultateChBean")
@SessionScoped
public class RezultateChestionarBean {
	private ChestionarTO chestionar;

	@EJB
	private ChestionarBI chestionarBI;
	@EJB
	private RaspunsBI raspunsBI;
	
	private RaspunsuriTO raspunsTO;
	private IntrebareTO intrebare;
	
	private List<RaspunsuriTO> raspunsuri;

	public ChestionarTO getChestionar() {

		if (chestionar == null) {
			FacesContext context = FacesContext.getCurrentInstance();
			String chestionarId = (String) context.getExternalContext()
					.getRequestParameterMap().get("rezultatePressed");
			this.chestionar = chestionarBI.getChestionar(Long
					.valueOf(chestionarId));

		}
		return chestionar;
	}

	public void setChestionar(ChestionarTO chestionar) {
		this.chestionar = chestionar;
	}

	public String afiseazaRaspunsuri() {
		return "raspunsuri";
	}

	public String back() {
		return "administrator";
	}

	public String percentage() {
		return null;

	}

	public String getNumberOfAnswers() {
		Integer nr = raspunsBI.getRaspNumberByChestionar(chestionar.getId());
		return nr.toString();

		/*
		 * 
		 * trebuie sa iau si id-ul intrebarii - ca sa rezulte cati au raspuns la
		 * intrebarea respectiva si apoi trebuie sa iau id-ul optiunii, ca sa
		 * calculez procentajul
		 */
	}
	
	public List<RaspunsuriTO> getRaspunsuri() {

		FacesContext context = FacesContext.getCurrentInstance();
		String chestId = (String) context.getExternalContext()
				.getRequestParameterMap().get("intrebareSelected");
		if (chestId != null)
			this.raspunsuri = raspunsBI.getRaspText(Long.valueOf(chestId));
		else
			this.raspunsuri = new ArrayList<RaspunsuriTO>();
		return this.raspunsuri;
	}

	public void setRaspunsuri(List<RaspunsuriTO> raspunsuri) {
		this.raspunsuri = raspunsuri;
	}
}
